package com.smartstate.ems.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.smartstate.ems.entity.RateCalculation;
import com.smartstate.ems.vo.RateCalculationTowVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>费率添加修改方法</p>
 * <p>@Author 陈永杰</p>
 * <p>@Date: 2022/8/29</p>
 * <p>@Time: 14:25</p>
 * <p>@version 1.0 <p>
 */
public class RateUtils {

    public String RateAddOrUpdate(List<Map> list) {

        List<Map<String, Object>> detailed = new ArrayList<>();
        RateCalculation rateCalculation = new RateCalculation();

        for (Map map : list) {
            List<Map> listAll = new ArrayList<>();
            Map<String, Object> mapItem = new HashMap();
            //拿到季度名称
            mapItem.put("quarter", map.get("quarter"));

            //拿到起止时间
            mapItem.put("timer", map.get("timer"));

            //拿到费率起止时间的[{}]
            JSONArray array = JSONArray.parseArray(JSON.toJSONString(map.get("rateTime")));
            List<RateCalculationTowVo> alarmDateVOS = array.toJavaList(RateCalculationTowVo.class);

            //.values之前是去重   之后是取到所有的去重之后的所有对象去排序 排序之后的集合
            List<RateCalculationTowVo> alarmDateVOSIndex = alarmDateVOS.stream().collect(Collectors.toMap
                    (RateCalculationTowVo::getPrice, node -> node, (n1, n2) -> n1)).values().stream().sorted
                    (Comparator.comparingDouble(RateCalculationTowVo::getPrice).reversed()).collect(Collectors.toList());


            //按照价格分组  1.1 1.1  1.2 1.2  1.3 .13
            Map<Double, List<RateCalculationTowVo>> alarmDateVOSMap = alarmDateVOS.stream().collect(Collectors.groupingBy(RateCalculationTowVo::getPrice));


            //如果list里 只有一条
            if (alarmDateVOSIndex.size() == 1) {
                alarmDateVOS.forEach(s -> s.setItem("平值"));
                if (alarmDateVOS.size() > 1) {
                    for (int i = 0; i < alarmDateVOS.size(); i++) {
                        Map mapFlat = new HashMap();
                        mapFlat.put("Price", alarmDateVOS.get(i).getPrice());
                        mapFlat.put("rateTimer", alarmDateVOS.get(i).getRateTimer());
                        mapFlat.put("item", alarmDateVOS.get(i).getItem());
                        mapFlat.put("index", alarmDateVOS.get(i).getIndex());
                        listAll.add(mapFlat);
                    }
                } else {
                    Map mapFlat = new HashMap();
                    mapFlat.put("Price", alarmDateVOS.get(0).getPrice());
                    mapFlat.put("rateTimer", alarmDateVOS.get(0).getRateTimer());
                    mapFlat.put("item", alarmDateVOS.get(0).getItem());
                    mapFlat.put("index", alarmDateVOS.get(0).getIndex());
                    listAll.add(mapFlat);
                }

                mapItem.put("rateTime", listAll);
                detailed.add(mapItem);

            } else if (alarmDateVOSIndex.size() == 2) {
                Double price = alarmDateVOSIndex.get(0).getPrice();
                alarmDateVOSMap.get(price).forEach(s -> s.setItem("峰值"));

                if (alarmDateVOSMap.get(price).size() > 1) {
                    for (int i = 0; i < alarmDateVOSMap.get(price).size(); i++) {
                        Map mapFlat = new HashMap();
                        mapFlat.put("Price", alarmDateVOSMap.get(price).get(i).getPrice());
                        mapFlat.put("rateTimer", alarmDateVOSMap.get(price).get(i).getRateTimer());
                        mapFlat.put("item", alarmDateVOSMap.get(price).get(i).getItem());
                        mapFlat.put("index", alarmDateVOSMap.get(price).get(i).getIndex());
                        listAll.add(mapFlat);
                    }
                } else {
                    Map mapPeak = new HashMap();
                    mapPeak.put("Price", alarmDateVOSMap.get(price).get(0).getPrice());
                    mapPeak.put("rateTimer", alarmDateVOSMap.get(price).get(0).getRateTimer());
                    mapPeak.put("item", alarmDateVOSMap.get(price).get(0).getItem());
                    mapPeak.put("index", alarmDateVOSMap.get(price).get(0).getIndex());
                    listAll.add(mapPeak);
                }


                Double priceIndex = alarmDateVOSIndex.get(1).getPrice();
                alarmDateVOSMap.get(priceIndex).forEach(s -> s.setItem("谷值"));

                if (alarmDateVOSMap.get(priceIndex).size() > 1) {
                    for (int i = 0; i < alarmDateVOSMap.get(priceIndex).size(); i++) {
                        Map mapFlat = new HashMap();
                        mapFlat.put("Price", alarmDateVOSMap.get(priceIndex).get(i).getPrice());
                        mapFlat.put("rateTimer", alarmDateVOSMap.get(priceIndex).get(i).getRateTimer());
                        mapFlat.put("item", alarmDateVOSMap.get(priceIndex).get(i).getItem());
                        mapFlat.put("index", alarmDateVOSMap.get(priceIndex).get(i).getIndex());
                        listAll.add(mapFlat);
                    }
                } else {
                    Map mapPeak = new HashMap();
                    mapPeak.put("Price", alarmDateVOSMap.get(priceIndex).get(0).getPrice());
                    mapPeak.put("rateTimer", alarmDateVOSMap.get(priceIndex).get(0).getRateTimer());
                    mapPeak.put("item", alarmDateVOSMap.get(priceIndex).get(0).getItem());
                    mapPeak.put("index", alarmDateVOSMap.get(priceIndex).get(0).getIndex());
                    listAll.add(mapPeak);
                }

                mapItem.put("rateTime", listAll);
                detailed.add(mapItem);
            } else {

                Double price = alarmDateVOSIndex.get(0).getPrice();
                alarmDateVOSMap.get(price).forEach(s -> s.setItem("峰值"));

                if (alarmDateVOSMap.get(price).size() > 1) {
                    for (int i = 0; i < alarmDateVOSMap.get(price).size(); i++) {
                        Map mapFlat = new HashMap();
                        mapFlat.put("Price", alarmDateVOSMap.get(price).get(i).getPrice());
                        mapFlat.put("rateTimer", alarmDateVOSMap.get(price).get(i).getRateTimer());
                        mapFlat.put("item", alarmDateVOSMap.get(price).get(i).getItem());
                        mapFlat.put("index", alarmDateVOSMap.get(price).get(i).getIndex());
                        listAll.add(mapFlat);
                    }
                } else {
                    Map mapPeak = new HashMap();
                    mapPeak.put("Price", alarmDateVOSMap.get(price).get(0).getPrice());
                    mapPeak.put("rateTimer", alarmDateVOSMap.get(price).get(0).getRateTimer());
                    mapPeak.put("item", alarmDateVOSMap.get(price).get(0).getItem());
                    mapPeak.put("index", alarmDateVOSMap.get(price).get(0).getIndex());
                    listAll.add(mapPeak);
                }

                Double priceIndex = alarmDateVOSIndex.get(alarmDateVOSIndex.size() - 1).getPrice();
                alarmDateVOSMap.get(priceIndex).forEach(s -> s.setItem("谷值"));

                if (alarmDateVOSMap.get(priceIndex).size() > 1) {
                    for (int i = 0; i < alarmDateVOSMap.get(priceIndex).size(); i++) {
                        Map mapFlat = new HashMap();
                        mapFlat.put("Price", alarmDateVOSMap.get(priceIndex).get(i).getPrice());
                        mapFlat.put("rateTimer", alarmDateVOSMap.get(priceIndex).get(i).getRateTimer());
                        mapFlat.put("item", alarmDateVOSMap.get(priceIndex).get(i).getItem());
                        mapFlat.put("index", alarmDateVOSMap.get(priceIndex).get(i).getIndex());
                        listAll.add(mapFlat);
                    }
                } else {
                    Map mapPeak = new HashMap();
                    mapPeak.put("Price", alarmDateVOSMap.get(priceIndex).get(0).getPrice());
                    mapPeak.put("rateTimer", alarmDateVOSMap.get(priceIndex).get(0).getRateTimer());
                    mapPeak.put("item", alarmDateVOSMap.get(priceIndex).get(0).getItem());
                    mapPeak.put("index", alarmDateVOSMap.get(priceIndex).get(0).getIndex());
                    listAll.add(mapPeak);
                }

                alarmDateVOSIndex.forEach(s -> {
                    if (!s.getPrice().equals(price) && !s.getPrice().equals(priceIndex)) {
                        alarmDateVOSMap.get(s.getPrice()).forEach(e -> e.setItem("平值"));

                        rateCalculation.setRateTime(alarmDateVOSMap.get(s.getPrice()));

                        if (alarmDateVOSMap.get(s.getPrice()).size() > 1) {
                            for (int i = 0; i < alarmDateVOSMap.get(s.getPrice()).size(); i++) {
                                Map mapFlat = new HashMap();
                                mapFlat.put("Price", alarmDateVOSMap.get(s.getPrice()).get(i).getPrice());
                                mapFlat.put("rateTimer", alarmDateVOSMap.get(s.getPrice()).get(i).getRateTimer());
                                mapFlat.put("item", alarmDateVOSMap.get(s.getPrice()).get(i).getItem());
                                mapFlat.put("index", alarmDateVOSMap.get(s.getPrice()).get(i).getIndex());
                                listAll.add(mapFlat);
                            }
                        } else {
                            Map mapPeak = new HashMap();
                            mapPeak.put("Price", alarmDateVOSMap.get(s.getPrice()).get(0).getPrice());
                            mapPeak.put("rateTimer", alarmDateVOSMap.get(s.getPrice()).get(0).getRateTimer());
                            mapPeak.put("item", alarmDateVOSMap.get(s.getPrice()).get(0).getItem());
                            mapPeak.put("index", alarmDateVOSMap.get(s.getPrice()).get(0).getIndex());
                            listAll.add(mapPeak);
                        }
                    }
                });
                mapItem.put("rateTime", listAll);
                detailed.add(mapItem);
            }

        }
        return JSON.toJSON(detailed).toString();
    }
}